import TabMenu from './data';
import { fetchIndexSysconfig } from '~/request/api';
Component({
  data: {
    active: TabMenu[0].url,
    list: [],
  },

  lifetimes: {
    ready() {
      this.indexSysconfig();
    },
  },

  methods: {
    onChange(event) {
      const url = event.detail.value;
      this.setData({ active: url });
      wx.switchTab({
        url: url.startsWith('/') ? url : `/${url}`,
      });
    },

    init() {
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
      this.setData({ active: route });
    },

    // 获取是否要显示定位导航
    async indexSysconfig() {
      let _TabMenu = TabMenu;
      // 当前路由
      const currentPages = getCurrentPages()?.[getCurrentPages().length - 1]?.route;
      let _position = true;
      // 在首页才进行请求接口
      if (currentPages === 'pages/home/home') {
        wx.removeStorageSync('isShowMap');
        try {
          const res = await fetchIndexSysconfig();
          wx.setStorageSync('isShowMap', JSON.stringify(res.position));
          _position = res.position;
        } catch (error) {
          console.log(error);
        }
      }
      const position = wx.getStorageSync('isShowMap') ? JSON.parse(wx.getStorageSync('isShowMap')) : true;
      // 首页优先取接口数据，其他页面取缓存
      if (!_position || !position) {
        _TabMenu = TabMenu.filter((item) => !['pages/map/index', 'pages/active/index'].includes(item.url));
      }
      this.setData({ list: _TabMenu });
    },
  },
});
